package com.example.service.impl;

import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;

import java.util.Map;

//bean的加载方式6
public class MyImportSelector implements ImportSelector {
    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
//        System.out.println("===================");
//        String className = importingClassMetadata.getClassName();
//        System.out.println("hasAnnotion?:"+importingClassMetadata.hasAnnotation("org.springframework.context.annotation.Configuration"));
//        System.out.println("className:"+className);
//        Map<String, Object> annotationAttributes = importingClassMetadata.getAnnotationAttributes("org.springframework.context.annotation.ComponentScan");
//        System.out.println(annotationAttributes);
//        System.out.println("=======================");

//        应用实例: 根据条件判断 如果有Configuration注解那么就加载Dog如果没有那么就加载Cat
        boolean flag = importingClassMetadata.hasAnnotation("org.springframework.context.annotation.Configuration");
        if (flag) {
            return new String[]{"com.example.bean.Dog"};
        }else {
            return new String[]{"com.example.bean.Cat"};
        }
    }}
